<?php
/**
 * Created by PhpStorm.
 * User: wenjoying@126.com
 * Date: 2018/5/31
 * Time: 10:16
 */

namespace App\Models;

use App\Http\Scopes\EnableScope;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Admin extends Authenticatable implements JWTSubject
{

    const COL_ID = 'id';
    const COL_UUID = 'uuid';
    const COL_IS_ENABLE = 'is_enable';

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * 自动应用全局作用域,限制返回有效结果
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new EnableScope());
    }

    /**
     * @desc 主键
     */
    protected $primaryKey = 'admin_id';

    /**
     * @desc 可填充字段
     */
    protected $fillable = [
        'user_type',
        'username',
        'password',
        'avatar',
        'mobile',
        'sex',
        'province',
        'city',
        'district',
        'abstract',
        'email',
        'is_enable',

    ];

    /**
     * @desc 隐藏字段
     */
    protected $hidden = ['password'];

    /**
     * @desc 查找字段
     */
    public $columns = [];

    /**
     * @desc 加密密码,使用orm时可以自动调用
     * @param $val
     * @return string
     */
    public function setPasswordAttribute($val)
    {
        return $this->attributes['password'] = bcrypt(encode_pwd($val));
    }

    /**
     * 头像附件
     */
    public function avatarAttach()
    {
        return $this->hasOne(Attach::class, 'id', 'avatar');
    }

}
